3 . "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
4 . "${_EXEC:-${0%/*}}/cgilite/storage.sh"
5 . "${_EXEC:-${0%/*}}/cgilite/json.sh"
7 [ "$_DATE" ] || _DATE="$(date +%s)"
9 debug "$REQUEST_METHOD $REQUEST_URI $SERVER_PROTOCOL $_DATE"
12 local J="$1" ztmp="${TMP:-/tmp}/zipfile_$$.zip"
15 # json_get "$J" parts.comments
17 case $(json_get "$J" title) in
19 printf %s "$content" |base64 -d
22 printf %s "$content" |base64 -d \
26 printf %s "$content" |base64 -d \
30 printf %s "$content" |base64 -d \
34 printf %s "$content" |base64 -d \
38 printf %s "$content" |base64 -d \
42 printf %s "$content" |base64 -d >"$ztmp"
43 unzip -qc "$ztmp" word/document.xml \
44 | head -c 128M | sed 's;<[^>]*>;;g'
48 printf %s "$content" |base64 -d >"$ztmp"
49 unzip -qc "$ztmp" xl/sharedStrings.xml \
50 | head -c 128M | sed 's;<[^>]*>; ;g'
54 printf %s "$content" |base64 -d >"$ztmp"
55 unzip -qc "$ztmp" content.xml \
56 | head -c 128M | sed 's;<[^>]*>;;g'
60 printf %s "$content" |base64 -d >"$ztmp"
61 unzip -qc "$ztmp" content.xml \
62 | head -c 128M | sed 's;<[^>]*>; ;g'
70 local index="$1" words w num total freq doc date J
73 words="$(printf %s\\n "$words" | awk '
74 BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
75 if ( length("ยก") == 1 ) # Utf-8 aware AWK
76 FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
78 FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
81 { for (n = 1; n <= NF; n++) printf "%s ", tolower($n); }
85 [ ! -f "${index}/$w" ] && continue
87 while read num total freq doc date; do
88 printf '%s-%i %f\n' "${doc}" "${date}" "$freq"
92 { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
93 END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
94 for (d in cnt) if ( cnt[d] == m ) printf "%f %s\n", weight[d], d;
98 | while read freq doc; do
99 date="${doc##*-}" doc="$(UNSTRING "${doc%-*}")"
101 if J="$(DBM "$_records" get "$doc")"; then
102 [ "$date" -eq "$(json_get obj:"$J" _indexdate)" ] \
103 && printf '%f %s %s\n' \
104 "$freq" "$(STRING "$doc")" "$(STRING "$J")"
109 _INDEX="${PATH_INFO#/}" _INDEX="${_INDEX%%/*}"
110 _records="${_DATA}/${_INDEX}/_0_DOCS"
112 if [ "$REQUEST_METHOD" = "PUT" ]; then
113 _doc="${PATH_INFO#"/${_INDEX}/_doc"}"
115 J="$(head -c "${CONTENT_LENGTH:-0}")"
116 # Don't use json parser to get content field
117 # Content can be very large and the json parser is slow
118 content="$(printf %s\\n "$J" |sed -E '
120 s;^.*,[ \t\r\n]*"content"[ \t\r\n]*:[ \t\r\n]*";;
124 J="$(printf %s\\n "$J" |sed -E '
126 s;,[ \t\r\n]*"content"[ \t\r\n]*:[ \t\r\n]*"[^"]*";;
128 J="$(json_load "${J}")"
130 debug "Content: ${#content} bytes"
131 debug "$(json_dump "$J")"
133 if [ "${#content}" -gt 0 ]; then
134 ingest "$J" "$content"\
135 | "${_EXEC}/concordance.sh" \
136 "$_DATA/$_INDEX/" "$(STRING "$_doc") $_DATE"
140 J="$(DB2 "$J" set _indexdate num:"$_DATE")"
142 if [ "${#content}" -eq 0 ]; then
143 printf '%s: %s\r\n' "Status" "200 OK"
145 elif DBM "$_records" insert "$_doc" "$J"; then
146 printf '%s: %s\r\n' "Status" "201 Created" "Location" "/${_INDEX}/_doc/$(URL "$_doc")" \
148 elif DBM "$_records" update "$_doc" "$J"; then
149 printf '%s: %s\r\n' "Status" "200 OK"
152 printf '%s\r\n' "Status: 500 Internal Server Error" ""
157 X-elastic-product: Elasticsearch
\r
158 content-type: application/vnd.elasticsearch+json;compatible-with=8
\r
160 { "_index": $(json_dump str:"${_INDEX}"),
161 "_id": $(json_dump str:"$_doc"),
168 elif [ "$REQUEST_METHOD" = "DELETE" ]; then
169 _doc="${PATH_INFO#"/${_INDEX}/_doc"}"
171 if DBM "$_records" get "$_doc"; then
172 if DBM "$_records" delete "$_doc"; then
173 printf '%s: %s\r\n' "Status" "200 OK"
176 printf '%s\r\n' "Status: 500 Internal Server Error" ""
180 printf '%s: %s\r\n' "Status" "404 Not Found"
185 X-elastic-product: Elasticsearch
\r
186 content-type: application/vnd.elasticsearch+json;compatible-with=8
\r
188 { "_index": $(json_dump str:"${_INDEX}"),
189 "_id": $(json_dump str:"$_doc"),
196 elif [ "$REQUEST_METHOD" = "POST" ]; then
197 J="$(json_load "$(head -c "${CONTENT_LENGTH:-0}")")"
198 J="$(json_get "$J" query.bool.must.bool.should)"
201 for j in $(DB2 "$J" iterate @); do
202 json_get "$(UNSTRING "$j")" match_phrase_prefix.content
207 search "${_DATA}/${_INDEX}" $words \
208 | while read -r score id source; do
209 S="$(DB2 "" set _index str:"${_INDEX}")"
210 S="$(DB2 "$S" set _id str:"$(UNSTRING "${id#/}")")"
211 S="$(DB2 "$S" set _score num:"$score")"
212 S="$(DB2 "$S" set _source obj:"$(UNSTRING "$source")")"
213 printf 'obj:%s\t' "$(STRING "$S")"
216 results="${results% }"
220 X-elastic-product: Elasticsearch
\r
221 Content-Type: application/vnd.elasticsearch+json;compatible-with=8
\r
225 "_shards":{"total":1,"successful":1,"skipped":0,"failed":0},
227 "total":{"value": $(DB2 "$results" count @) ,"relation":"eq"},
228 "max_score": $(json_get "arr:$results" '[0]._score' 2>&- || printf 0),
229 "hits": $(json_dump "arr:$results")
234 elif [ "$REQUEST_METHOD" = "HEAD" ]; then
235 accept="$(HEADER Accept)"
236 [ ! "${accept#*"vnd.elasticsearch+json"*}" ] \
237 && ctype="${accept}" || ctype="application/json"
241 X-elastic-product: Elasticsearch
\r
242 content-type: ${ctype}
\r
247 elif [ "$REQUEST_METHOD" = "GET" ]; then
248 accept="$(HEADER Accept)"
249 [ ! "${accept#*"vnd.elasticsearch+json"*}" ] \
250 && ctype="${accept}" || ctype="application/json"
254 X-elastic-product: Elasticsearch
\r
255 content-type: ${ctype}
\r
259 if [ "$PATH_INFO" = "/${_INDEX}/" ]; then
261 { $(json_dump str:"${_INDEX}"): {
265 "content": {"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
266 "hash":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
267 "metatags":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
268 "owner":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
269 "parts":{"properties":{"comments":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},
270 "provider":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
271 "share_names":{"properties":{"paul":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},
272 "source":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},
273 "title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
278 "routing":{"allocation":{"include":{"_tier_preference":"data_content"}}},
279 "number_of_shards":"1",
280 "provided_name": $(json_dump str:"${_INDEX}"),
281 "creation_date": "$(stat -c %W "${_DATA}/${_INDEX}")",
282 "number_of_replicas":"1",
283 "uuid":"0000000000000000000000",
284 "version":{"created":"8500010"}
293 "cluster_name" : "elasticsearch",
296 "lucene_version" : "9.9.2",
297 "minimum_wire_compatibility_version" : "7.17.0",
298 "minimum_index_compatibility_version" : "7.0.0"
300 "tagline" : "You Know, for Search"
307 printf '%s\r\n' "Status: 500 Internal Server Error" ""